import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { FormBuilder, FormGroup } from '@angular/forms';
import { AuthDictService } from '../../../../system/dict/service/dict.service';
import { TransportService } from '../../../transport-service/transport.service';
import { BasicDataService } from '../../../basic-data/basic-data.service';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { ShipStatService } from '../../ship-stat.service';
import { TallyCheckOstListComponent } from '../../../tally-check/check/ost/list/list.component';
import { ExportShipStatListComponent } from '../list/list.component';
import { deepCopy } from '@delon/util';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less']
})
export class ExportShipStatIndexComponent implements OnInit {

	@ViewChild('statList')
	shipStatListComponent: ExportShipStatListComponent;

	selectedShip: any;

	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: false,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;

	searchForm: FormGroup;
	shipList = [];

	dotColor = {
		Y: '#32CD32',
		M: '#0000FF',
		E: '#f5c30f',
		L: '#808080',
		NULL: '#808080',
	};

	columns: STColumn[] = [
		{
			title: '船信息',
			index: 'expShipInfo',
			render: 'expShipInfo',
			width: 120
		},
		{
			title: '泊位',
			index: 'berthName',
			width: 60
		}
	];
	dataList = [];

	constructor(
		private fb: FormBuilder,
		private dictService: AuthDictService,
		private transportService: TransportService,
		private shipStatService: ShipStatService
	) {
		this.transportService.transportShipList({}).subscribe(res => {
			this.shipList = res;
		});
		this.searchForm = this.fb.group({
			transportShipId: null,
		});

	}

	ngOnInit(): void {
		this.loadData();
	}

	loadData(): any {
		const params = {
			...this.searchForm.value,
			ie: "E",
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};

		this.pageInfo.loading = true;
		this.shipStatService.shipListForShipStatPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			if (this.dataList.length > 0) {
				this.selectedShip = this.dataList[0];
				this.shipStatListComponent.loadData(this.selectedShip.transportShipId);
			}
		});
	}

	search(): any {
		this.loadData();
	}

	reset() {
		this.searchForm.reset();
		this.loadData();
	}

	searchShipList(data: string) {
		this.transportService.transportShipList({ keyword: data }).subscribe(res => {
			this.shipList = res;
		});
	}

	stChange(ret: STChange) {
		console.log(ret);
		if (ret.type === 'click') {
			if (ret.click.item) {
				const item = deepCopy(ret.click.item);
				this.selectedShip = {
					transportShipId: item.transportShipId,
					shipNameZh: item.shipNameZh,
					expVoyage: item.expVoyage,
					rtb: item.rtb,
					rtd: item.rtd
				}
				this.shipStatListComponent.loadData(item.transportShipId);
			}
		}
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

}
